package whf.shop.tag;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import whf.framework.log.Log;
import whf.framework.log.LogFactory;
import whf.framework.util.BeanUtils;
import whf.framework.util.StringUtils;
import whf.framework.web.tag.HtmlTag;
import whf.shop.entity.Currency;
import whf.shop.service.CurrencyServiceImp;
import whf.shop.utils.ShopUtils;

/**
 *  显示货币的tag
 * @author wanghaifeng
 * @create Jan 12, 2007 10:14:46 AM
 * 
 */
public class CurrencyTag extends HtmlTag {
	private static Log log = LogFactory.getLog(CurrencyTag.class);

	private final static long serialVersionUID = 0;

	private String clazz;

	private String format = "0.00";

	private double input;

	/**
	 * 获取数据来源，使用点号分开
	 * 第一个表示从request或者session中获取对象key值，其它一次表示属性组
	 * @property String:inputProperty
	 */
	private String dataSourceName;

	public void setClazz(String clazz) {
		super.setClazz(clazz);
		this.clazz = clazz;
	}

	public void setDataSourceName(String dataSourceName) {
		this.dataSourceName = dataSourceName;
	}

	public void setInput(double inputValue) {
		this.input = inputValue;
	}

	public void setFormat(String format) {
		this.format = format;
	}

	public int doStartTag() throws JspException {
		HttpServletRequest request = (HttpServletRequest) pageContext
				.getRequest();
		Currency curCurrency = null;
		if (ShopUtils.getCurrentMember(request.getSession()) != null) {
			curCurrency = ShopUtils.getCurrentMember(request.getSession())
					.getCurrency();
		}
		if(curCurrency == null){
			curCurrency = (Currency)request.getSession().getAttribute("SelectOnFrontPage");;
		}
		//
		Currency baseCurrency = (Currency) super.pageContext
				.getServletContext().getAttribute(
						Currency.class.getName() + ":BaseCurrency");
		try {
			if (baseCurrency == null) {
				baseCurrency = CurrencyServiceImp.getCurrencyService()
						.findBaseCurrency();
				super.pageContext.getServletContext().setAttribute(
						Currency.class.getName() + ":BaseCurrency",
						baseCurrency);
			}
			if (curCurrency == null) {
				curCurrency = baseCurrency;
			}
			// display
			if (!StringUtils.isEmpty(this.dataSourceName)) {
				String[] ss = StringUtils.split(this.dataSourceName, ".");
				Object obj = request.getAttribute(ss[0]);
				if (obj == null) {
					obj = request.getSession().getAttribute(ss[0]);
				}
				if (ss.length == 1) {
					try {
						this.input = Double.parseDouble(obj.toString());
					} catch (Exception e) {
						log.info(e);
					}
				} else {
					String s = ss[1];
					for (int i = 2; i < ss.length; i++) {
						s += "." + ss[i];
					}
					try {
						String v = (String) BeanUtils.getStringProperty(obj, s);
						this.input = Double.parseDouble(v);
					} catch (Exception e) {
						log.info(e);
					}
				}
			}
			double value = this.input * curCurrency.getExchangeRate()
					/ baseCurrency.getExchangeRate();
			NumberFormat nf = new DecimalFormat(format);
			JspWriter out = pageContext.getOut();
			if (!StringUtils.isEmpty(this.clazz)) {
				this.clazz = "class=\"" + this.clazz + "\"";
			}
			out.print("<span " + this.clazz + ">");
			out.print(curCurrency.getSymbol());
			out.print(nf.format(value));
			out.print("</span>");
		} catch (Exception e) {
			log.info(e);
		}
		this.release();
		return SKIP_BODY;
	}

	public void release(){
		super.release();
		this.clazz = "";
		this.input = 0;
		this.format = "0.00";
		this.dataSourceName = "";
	}
}
